package com.kmist.xiakexing.fragments;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kmist.xiakexing.R;
import com.kmist.xiakexing.adapter.CommonAdapter;
import com.kmist.xiakexing.adapter.ViewHolder;
import com.kmist.xiakexing.base.BaseFragment;
import com.kmist.xiakexing.interfaces.IMainWorldViewCallback;
import com.kmist.xiakexing.presenters.MainWorldPresenter;
import com.kmist.xiakexing.presenters.NpcPresenter;
import com.kmist.xiakexing.presenters.PlayerPresenter;
import com.kmist.xiakexing.tools.Config;
import com.kmist.xiakexing.tools.OnMultiClickListener;
import com.kmist.xiakexing.utils.MidEnemyBean;
import com.kmist.xiakexing.utils.scene.GameScene;
import com.kmist.xiakexing.utils.MapExitBean;
import com.kmist.xiakexing.utils.scene.ItemBean;
import com.kmist.xiakexing.utils.UILoader;
import com.kmist.xiakexing.utils.scene.NpcBean;
import com.kmist.xiakexing.view.NpcMoreInfoDialog;

import java.util.ArrayList;
import java.util.List;


public class MainWorldFragment extends BaseFragment implements IMainWorldViewCallback {

    private static final String TAG = "MainWorldFragment";
    private View view;
    private TextView property_text_name;
    private ImageButton property_imagebtn_head;
    private ProgressBar property_progress_evolution;
    private TextView property_text_evolution;
    private TextView property_text_jinbi;
    private TextView property_text_level;
    private TextView curent_map_name;
    private RecyclerView map_items;
    private RecyclerView map_npc;
    private RecyclerView map_enemy;

    private TextView mapinfo;
    private TextView npc_greeting;

    private List<MapExitBean> mapExitsView;
    private List<MidEnemyBean> mapEnemyView;
    private List<NpcBean> mapNpcView;
    private List<ItemBean> mapItemsView;

    private MainWorldPresenter mainWorldPresenter;
    private UILoader mUiLoader;
    private RecyclerView mControlBar;
    private CommonAdapter<MapExitBean> mapExitAdapter;
    private CommonAdapter<ItemBean> mapItemAdapter;
    private CommonAdapter<NpcBean> mapNpcAdapter;
    private CommonAdapter<MidEnemyBean> mapEnemyAdapter;
    private PlayerPresenter playerPresenter;
    private NpcPresenter npcPresenter;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //取消接口注册
        if (mainWorldPresenter != null) {
            mainWorldPresenter.unRegisterViewCallback(this);
        }
    }

    @Override
    protected View onSubViewCreate(LayoutInflater layoutInflater, ViewGroup container) {

        mUiLoader = new UILoader(getContext()) {
            @Override
            protected View getSuccessView(ViewGroup container) {
                return createSuccessView(layoutInflater,container);
            }
        };

        //加载子控件
        mapExitsView = new ArrayList<>();
        mapEnemyView = new ArrayList<>();
        mapNpcView = new ArrayList<>();
//        mapItemsView = new ArrayList<>();

        mainWorldViewInit();
        playerPresenter = PlayerPresenter.getInstance();
        npcPresenter = NpcPresenter.getsInstance();
        mainWorldPresenter = MainWorldPresenter.getInstance();
        mainWorldPresenter.registerViewCallback(this);
        mainWorldPresenter.getPropertyInfo(); //获取玩家属性
        mainWorldPresenter.getGameSence(playerPresenter.getNowmid());


        if (mUiLoader.getParent() instanceof ViewGroup) {
            ((ViewGroup) mUiLoader.getParent()).removeView(mUiLoader);
        }
        return mUiLoader;
    }

    private View createSuccessView(LayoutInflater layoutInflater, ViewGroup container) {
        // view 加载完成
        view = layoutInflater.inflate(R.layout.fragment_mainworld, container, false);
        return view;
    }

    private void mainWorldViewInit() {

        /* 头像栏 */
        property_text_name = view.findViewById(R.id.property_text_name);
        property_imagebtn_head = view.findViewById(R.id.property_imagebtn_head);
        property_progress_evolution = view.findViewById(R.id.property_progress_evolution);
        property_text_evolution = view.findViewById(R.id.property_text_evolution);
        property_text_level = view.findViewById(R.id.property_text_level);
        property_text_jinbi = view.findViewById(R.id.property_text_jinbi);

        /* 地图物品栏 */
        curent_map_name = view.findViewById(R.id.map_name);

        map_items = view.findViewById(R.id.map_items);
        map_npc = view.findViewById(R.id.map_npc);
        map_enemy = view.findViewById(R.id.map_enemys);
        showMapEnemy(false, false);
        showMapNpc(false, false);
        //showMapItem(true, false);

        /* 地图信息栏 */
        mapinfo = view.findViewById(R.id.mapinfo);
        npc_greeting = view.findViewById(R.id.npc_greeting);

        /* 地图控制器 adapter */
        mControlBar = view.findViewById(R.id.controlbar_recyclerview);
        showControlBar(true,false);
    }

    private void showMapEnemy(boolean isVertical, boolean isRevers) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(Config.context);
        layoutManager.setOrientation(isVertical? LinearLayoutManager.VERTICAL : LinearLayoutManager.HORIZONTAL);
        layoutManager.setReverseLayout(isRevers);

        map_enemy.setLayoutManager(layoutManager);
        mapEnemyAdapter = new CommonAdapter<MidEnemyBean>(Config.context, R.layout.enemy_map_info_view, mapEnemyView) {
            @Override
            protected void convert(ViewHolder holder, MidEnemyBean midEnemyBean) {
                holder.setText(R.id.map_info_enemy, midEnemyBean.getName());

                holder.setOnClickListener(R.id.map_info_enemy, new OnMultiClickListener() {
                    @Override
                    public void onMultiClick(View v) {
                        Config.Alarm(midEnemyBean.toString());
                    }
                });
            }
        };
        map_enemy.setAdapter(mapEnemyAdapter);
    }

    private void showMapNpc(boolean isVertical, boolean isRevers) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(Config.context);
        layoutManager.setOrientation(isVertical? LinearLayoutManager.VERTICAL : LinearLayoutManager.HORIZONTAL);
        layoutManager.setReverseLayout(isRevers);

        map_npc.setLayoutManager(layoutManager);
        mapNpcAdapter = new CommonAdapter<NpcBean>(Config.context, R.layout.npc_map_info_view, mapNpcView) {

            @Override
            protected void convert(ViewHolder holder, NpcBean npcBean) {
                holder.setText(R.id.map_info_npc, npcBean.getName());
                if(!npcBean.getGreet().equals("0"))
                {
                    npc_greeting.setText(npcPresenter.getGreeting(npcBean, playerPresenter.getPlayerd()));
                }
                /*TODO 根据任务设置人物名字颜色 */
                if(npcBean.getTaskIds()!= ""){
                    holder.setTextColor(R.id.map_info_npc, Color.RED);
                }
                holder.setOnClickListener(R.id.map_info_npc, new OnMultiClickListener() {
                    @Override
                    public void onMultiClick(View v) {
                        //itemInfo.setText(npcBean.getDesc());
                        //先获取npc详细信息 阻塞
                        mainWorldPresenter.getMoreNpcInfo(npcBean);

                        new NpcMoreInfoDialog(getContext(), npcBean);
                    }
                });


            }
        };
        map_npc.setAdapter(mapNpcAdapter);
    }
//
//    private void showMapItem(boolean isVertical, boolean isRevers) {
//        LinearLayoutManager layoutManager = new LinearLayoutManager(Config.context);
//        layoutManager.setOrientation(isVertical? LinearLayoutManager.VERTICAL : LinearLayoutManager.HORIZONTAL);
//        layoutManager.setReverseLayout(isRevers);
//        map_items.setLayoutManager(layoutManager);
//
//        mapItemAdapter = new CommonAdapter<ItemBean>(Config.context, R.layout.item_map_info_view, mapItemsView) {
//            @Override
//            protected void convert(ViewHolder holder, ItemBean itemBean) {
//                holder.setText(R.id.map_info_item, itemBean.getName());
//                holder.setOnClickListener(R.id.map_info_item, new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        itemInfo.setText(itemBean.getDesc());
//                    }
//                });
//            }
//        };
//
//        map_items.setAdapter(mapItemAdapter);
//    }
//
    private void showControlBar(boolean isVertical, boolean isRevers) {

        //设置样式--布局管理器
        GridLayoutManager layoutManager = new GridLayoutManager(Config.context,4);
        //设置布局管理器控制正向反向，垂直横向
        layoutManager.setOrientation(isVertical? LinearLayoutManager.VERTICAL : LinearLayoutManager.HORIZONTAL);
        layoutManager.setReverseLayout(isRevers);
        mControlBar.setLayoutManager(layoutManager);

        mapExitAdapter = new CommonAdapter<MapExitBean>(Config.context, R.layout.item_controlbar_view, mapExitsView) {
            @Override
            protected void convert(ViewHolder holder, MapExitBean mapExitBean) {
                holder.setText(R.id.controlbar_item, mapExitBean.getName());
                holder.setOnClickListener(R.id.controlbar_item, new OnMultiClickListener() {
                    @Override
                    public void onMultiClick(View v) {
                        /* 更新地图 */
                        Log.d(TAG, "click:" + mapExitBean.getName());
                        mainWorldPresenter.getGameSence(mapExitBean.getMid());
                        mapExitAdapter.notifyDataSetChanged();
                    }
                });
            }

        };
        mControlBar.setAdapter(mapExitAdapter);
    }


    @Override
    public void onResultSuccess(GameScene gameScene) {
        curent_map_data_update(gameScene); //更新地图数据
        curent_map_enemy_update(gameScene);
        curent_map_npc_update(gameScene);
        //curent_map_item_update(gameScene);

        curent_map_exit_update(gameScene);
        mUiLoader.updateStatus(UILoader.UIStatus.SUCCESS);
    }

    private void curent_map_npc_update(GameScene gameScene) {
        mapNpcView.clear();
        npc_greeting.setText("");
        if(gameScene.getNpcList() != null){
            mapNpcView.addAll(gameScene.getNpcList());
        }
        mapNpcAdapter.notifyDataSetChanged();
    }

    private void curent_map_enemy_update(GameScene gameScene) {
        mapEnemyView.clear();
        if(gameScene.getEnemyList() != null){
            mapEnemyView.addAll(gameScene.getEnemyList());
        }
        mapEnemyAdapter.notifyDataSetChanged();
    }

    @Override
    public void onResultSuccess(String msg) {
        Log.d(TAG, msg);
    }


    private void curent_map_data_update(GameScene gameScene) {
        curent_map_name.setText(gameScene.getMap().getName());
        mapinfo.setText(gameScene.getMap().getDesc());
    }

//    private void curent_map_item_update(MapBean map) {
//        mapItemsView.clear();
//        itemInfo.setText("");
//        mapItemsView.addAll(map.getItemBeans());
//        mapItemAdapter.notifyDataSetChanged();
//    }

    private void curent_map_exit_update(GameScene gameScene) {
        mapExitsView.clear();
        mapExitsView.addAll(gameScene.getMap().getMapExits());
        mapExitAdapter.notifyDataSetChanged();
        // northwest north  northeast up
        // west      cur    east      down
        // southwest south  southeast enter
    }

    @Override
    public void onNetworkError() {
        mUiLoader.updateStatus(UILoader.UIStatus.NETWORK_ERROR);
    }

    @Override
    public void onResultEmpty() {
        mUiLoader.updateStatus(UILoader.UIStatus.EMPTY);
    }

    @Override
    public void onResultLoading() {
        mUiLoader.updateStatus(UILoader.UIStatus.LOADING);
    }

    @Override
    public void onClick(View v) {

    }
}
